<template>
  <div>
    <div class="top_bar">
      <van-search
        v-model="name"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="onCancel"
        @keyup="onkeyup"
      />
    </div>
    <div class="hbox" style="height: 3.85rem"></div>
    <div class="vide_content">
      <div class="vide_item" v-for="item in videoList" :key="item.vidID">
        <div class="vide_top">
          <router-link
            :to="{ name: 'videDetail', params: { vid: item.vidID } }"
          >
            <div v-if="item.Cover != ''">
              <img
                style="border-radius: 10px"
                v-lazy="evImage + item.Cover"
                alt=""
              />
            </div>

            <video v-else :src="evImage + item.item.Cover"></video>
          </router-link>
        </div>
        <p class="vide_title">
          <van-tag
            style="margin-right: 6px"
            v-if="item.MediaType == '视频'"
            plain
            color="#7232dd"
            >视频</van-tag
          >{{ item.message }}
        </p>
        <div class="vide_bottom">
          <!-- 头像与昵称 -->
          <div class="tip_l">
            <img src="../../assets/images/k.png" alt="" />
            <span>万能青年</span>
          </div>
          <!-- 点赞 -->
          <div class="tip_r">
            <van-icon
              name="like-o"
              size="18"
              @click="likeClick"
              :color="num ? '#F1050A' : ''"
            />
            <transition
              @before-enter="beforeEnter"
              @enter="enter"
              @after-enter="afterEnter"
            >
              <span class="addOne" v-if="isLike">+1</span>
            </transition>

            {{ num }}
          </div>
        </div>
      </div>
    </div>
    <transition name="van-slide-up">
      <div class="PrepBox" v-if="PrepBoxShow">
        <ul>
          <li
            v-for="item in wordsList"
            :key="item.sid"
            class="van-hairline--bottom"
          >
           <a href="#" @click.prevent="wordeSubmit(item.Words)">
            {{ item.Words }}
            </a>
          </li>
        </ul>
      </div>
    </transition>
  </div>
</template>
<script>
export default {
  data() {
    return {
      name: "",
      loading: false,
      finished: false,
      videoList: [],
      PrepBoxShow: false,
      wordsList: [],
      totol: 0,
      page_num: 0,
      start: 1,
      num: "22", //点赞数
      isLike: false,
      Sname: "",
       time:null,
    };
  },
  created() {},
  methods: {
    async onSearch() {
      this.getSearchReults();
      const { data: res } = await this.$http.get("ValidationWords", {
        params: { words: this.name },
      });

      if (res.status === 200) {
        this.addWords();
      }
      if (res.status === 202) {
        this.addHotwords();
      }
    },
    async addWords() {
      let data = {
        Words: this.name,
        hot: 1,
      };
      const { data: res } = await this.$http.post("addWords", data);
    },
    async addHotwords() {
      let data = {
        Words: this.name,
      };
      const { data: res } = await this.$http.post("addHot", data);
    },
    onCancel() {
      this.$router.push("/discove");
    },
    async onkeyup() {
      if (this.name === "") {
        this.PrepBoxShow = false;
      } else {
        this.PrepBoxShow = true;
        if(this.time!==null){
            clearTimeout(this.time)
        }
       this.time= setTimeout(()=>{
           this.searchWords()
        },500)
      }
    },
    async searchWords(){
        const { data: res } = await this.$http.get("searchWords", {
          params: { title: this.name },
        });
        console.log(res)
        this.wordsList = res.data;
    },
    wordeSubmit(item){
        this.PrepBoxShow=false
        this.getSearchReults();
    },
    getview() {
      this.name = this.$route.params.Sname;
      this.getSearchReults();
    },
    async getSearchReults() {
      const { data: res } = await this.$http.get("searchVideo", {
        params: { title: this.name },
      });
      console.log(res);
      this.videoList = res.data;
    },
    beforeEnter(el) {
      //动画开始进入前
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      //动画执行时
      el.offsetWidth;
      el.style.transform = "translate(5px,-20px)";
      el.style.transition = "all 0.6s ease";
      done();
    },
    afterEnter(el) {
      //动画结束时
      this.isLike = !this.isLike;
    },
    likeClick() {
      this.isLike = true;
      this.num++;
    },
  },
  watch: {
    $route: {
      handler(newVal, oldVal) {
        this.getview(newVal, oldVal);
      },
      deep: true,
      immediate: true,
    },
  },
};
</script>
<style lang="less" scoped>
.top_bar {
  position: fixed;
  width: 100%;
  max-width: 640px;
  top: 0;
  background: #fff;
  z-index: 1500;
  border-bottom: 1px solid #eee;
}
.vide_content {
  overflow: hidden;
  column-count: 2;
  .vide_item {
    width: 100%;
    -moz-column-break-inside: avoid;
    break-inside: avoid;
    box-sizing: border-box;
    padding: 5px;
    /* margin: 5px; */
    margin-bottom: 10px;
    box-shadow: 1px 2px 5px 2px #eaeaea;
    background: white;
    border-radius: 10px;
  }
  .vide_top {
    // background: black;
    border-radius: 10px;
    video {
      width: 100%;
      height: 100%;
      border-radius: 10px;
    }
  }
  .vide_title {
    height: 2rem;
    font-size: 0.9rem;
    padding-top: 0.5rem;
    color: #1a1a1a;
    line-height: 1.3rem;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    padding: 6px;
    /* padding-bottom: 12px; */
    margin-bottom: 10px;
  }
  .vide_bottom {
    margin-bottom: 26px;
    padding: 1px;
  }
  .tip_l {
    float: left;
    display: flex;
    align-items: center;
    img {
      width: 25px;
      height: 25px;
      border-radius: 50%;
    }
    span {
      margin-left: 3px;
    }
  }
  .tip_r {
    margin-top: 5px;
    float: right;
    display: flex;
    align-items: center;
    .van-rate {
      width: 20px;
      height: 20px;
      border-radius: 50%;
    }
    span {
      margin-left: 3px;
      color: #bcbbbb;
    }
  }
}
.PrepBox {
  width: 100%;
  height: 100%;
  position: absolute;
  background: #ffffff;
  top: 50px;
  left: 0;
  ul {
    overflow: hidden;
    li {
      padding: 5px;
      text-align: left;
      line-height: 20px;
    }
  }
}
</style>